GBML manual / Lists

list_shuffle

Mixes up the order of the items in target, in place: afterwards it holds the same items in a random order. It makes no copy. Any kind of item can be shuffled, text included.

Syntax

list_shuffle(target)

Arguments

NameType
targetlist

Returns

Nothing.

Description

Mixes up the order of the items in target, a list. Afterwards the list holds exactly the same items as before, in a random order. It is the way to shuffle a deck of cards.

list_shuffle(deck);

It changes the list itself

list_shuffle moves the items that are already in target. It does not make a new list, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with list_copy first and shuffle the one you want mixed up.

It mixes the items the list holds right now, the number list_length gives you. The empty places up to the capacity are not touched. A list with no items or with one item is left as it is.

Any kind of item can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the list holds. (A sort is different: it only works on numbers and text.)

Where the random numbers come from

A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:

Every possible order can come up. It is very slightly more likely to land on some orders than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.

What happens in the awkward cases

You do thisWhat happens
shuffle a list with no items or one itemnothing changes, and no random numbers are used
shuffle a list that has the same item more than onceworks as normal. Equal items are interchangeable, so you cannot tell which is which
shuffle and hope an item movesit may stay where it was. That can happen with random order, and it gets less likely the more items there are
give it an array or a gridrefused when checked, and the message names the function that shuffles that kind, like array_shuffle
give it a stack or a queuerefused when checked. Their order is what they are for, so they have no shuffle
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes a list as a parameter works on the caller's own list (see Lists). So list_shuffle inside a script shuffles the caller's list.

What it costs

Nothing is set aside: the items are swapped where they stand, one swap for each item after the first. No timing was measured.

Example

/// @type {list<string>}
/// @capacity 4
var deck = ["ace", "king", "queen", "jack"];
roll_reseed(7);        // the same shuffle every time this line runs
list_shuffle(deck);    // deck holds the same four cards in a new order
var top = deck[0];     // whichever card landed first

On the GBA

Shuffles the list_length(target) items the list holds - never its capacity. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.

See also: array_shufflegrid_shufflelist_sortlist_copylist_lengthroll_introll_reseedListsData containers